<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="80px">
    <el-form-item label="旧密码" prop="oldPassword">
      <el-input v-model="user.oldPassword" placeholder="请输入旧密码" type="password" show-password/>
    </el-form-item>
    <el-form-item label="新密码" prop="newPassword">
      <el-input v-model="user.newPassword" placeholder="请输入新密码" type="password" show-password/>
      <div class="password-tips">
        <p>密码要求：</p>
        <ul>
          <li>长度8-20位</li>
          <li>必须包含大写字母、小写字母、数字和特殊字符</li>
          <li>不能包含连续字符(如123、abc)</li>
          <li>不能包含重复字符(如aaa、111)</li>
          <li>不能包含键盘序列(如qwerty、123456)</li>
          <li>不能使用常见弱密码</li>
        </ul>
      </div>
    </el-form-item>
    <el-form-item label="确认密码" prop="confirmPassword">
      <el-input v-model="user.confirmPassword" placeholder="请确认密码" type="password" show-password/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserPwd } from "@/api/system/user";

export default {
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error("新密码不能为空"));
        return;
      }
      
      // 检查密码长度
      if (value.length < 8 || value.length > 20) {
        callback(new Error("密码长度必须在8-20位之间"));
        return;
      }
      
      // 检查是否包含大写字母
      if (!/[A-Z]/.test(value)) {
        callback(new Error("密码必须包含至少一个大写字母"));
        return;
      }
      
      // 检查是否包含小写字母
      if (!/[a-z]/.test(value)) {
        callback(new Error("密码必须包含至少一个小写字母"));
        return;
      }
      
      // 检查是否包含数字
      if (!/\d/.test(value)) {
        callback(new Error("密码必须包含至少一个数字"));
        return;
      }
      
      // 检查是否包含特殊字符
      if (!/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>/?]/.test(value)) {
        callback(new Error("密码必须包含至少一个特殊字符"));
        return;
      }
      
      // 检查是否包含连续字符
      for (let i = 0; i < value.length - 2; i++) {
        const c1 = value.charCodeAt(i);
        const c2 = value.charCodeAt(i + 1);
        const c3 = value.charCodeAt(i + 2);
        if (c2 === c1 + 1 && c3 === c2 + 1) {
          callback(new Error("密码不能包含连续字符"));
          return;
        }
      }
      
      // 检查是否包含重复字符
      for (let i = 0; i < value.length - 2; i++) {
        if (value[i] === value[i + 1] && value[i + 1] === value[i + 2]) {
          callback(new Error("密码不能包含重复字符"));
          return;
        }
      }
      
      // 检查是否包含键盘序列
      const keyboardSequences = [
        'qwerty', 'asdfgh', 'zxcvbn', '123456', '654321', '111111', '000000',
        'password', 'admin', 'root', 'user', 'abc123', '123abc', 'qwe123', '123qwe'
      ];
      const lowerValue = value.toLowerCase();
      for (const sequence of keyboardSequences) {
        if (lowerValue.includes(sequence)) {
          callback(new Error("密码不能包含键盘序列"));
          return;
        }
      }
      
      callback();
    };
    
    return {
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      },
      // 表单校验
      rules: {
        oldPassword: [
          { required: true, message: "旧密码不能为空", trigger: "blur" }
        ],
        newPassword: [
          { required: true, validator: validatePassword, trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUserPwd(this.user.oldPassword, this.user.newPassword).then(response => {
            this.$modal.msgSuccess("修改成功");
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    }
  }
};
</script>

<style scoped>
.password-tips {
  margin-top: 10px;
  padding: 10px;
  background-color: #f5f7fa;
  border-radius: 4px;
  font-size: 12px;
  color: #606266;
}

.password-tips p {
  margin: 0 0 5px 0;
  font-weight: bold;
}

.password-tips ul {
  margin: 0;
  padding-left: 20px;
}

.password-tips li {
  margin: 2px 0;
}
</style>
